package com.rayson.yututu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rayson.yututu.annotation.AuthCheck;
import com.rayson.yututu.common.BaseResponse;
import com.rayson.yututu.constant.UserConstant;
import com.rayson.yututu.model.entity.User;
import com.rayson.yututu.model.req.user.UserLoginReq;
import com.rayson.yututu.model.req.user.UserQueryReq;
import com.rayson.yututu.model.req.user.UserRegistReq;
import com.rayson.yututu.model.vo.LoginUserVO;
import com.rayson.yututu.model.vo.UserVO;
import com.rayson.yututu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Author: rayson
 * Description:
 * Version: 1.0
 * Date: 2024-12-18 21:08
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {


    @Resource
    private UserService userService;


    @PostMapping("/regist")
    public BaseResponse<String> regist(@RequestBody @Valid UserRegistReq userRegistReq) {
        return BaseResponse.success(userService.regist(userRegistReq));
    }

    @PostMapping("/login")
    public BaseResponse<LoginUserVO> login(@RequestBody @Valid UserLoginReq userLoginReq, HttpServletRequest request) {
        return BaseResponse.success(userService.login(userLoginReq, request), "登录成功");
    }

    @GetMapping("/loginUser")
    public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        LoginUserVO loginUserVO = new LoginUserVO();
        BeanUtils.copyProperties(loginUser, loginUserVO);
        return BaseResponse.success(loginUserVO);
    }

    @PostMapping("/logout")
    public BaseResponse<Void> logout(HttpServletRequest request) {
        userService.logout(request);
        return BaseResponse.success(null, "已登出");
    }

    @GetMapping("/list")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVO>> getUserVOPage(UserQueryReq userQueryReq) {
        return BaseResponse.success(userService.queryUserVOList(userQueryReq));
    }

    // 管理员创建用户

    // 管理员根据id删除用户
    @DeleteMapping("/{userId}")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Void> deleteUserById(@PathVariable("userId") String userId) {
        boolean flag = userService.removeById(userId);
        log.info("删除用户{}，结果：{}", userId, flag);
        return BaseResponse.success(null, "删除成功");
    }
    // 更新用户


}
